﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Customer
{
    public class CustomerMoreController : PageInfoBase
    {
        [ActionFilter(IsLogin = true)]       
        public ActionResult Index(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return Content(WebMessage.NoGrant);
            String sql = String.Format(@"select 
    t1.*,t5.*,t6.*,t7.*,t9.SignSl,t8.WeekHksl,t8.Zsl,t10.Hdsl,case t1.custstate when 6 then case when t1.ContractId is null then '新会员待开课' else '开课中' end  else t4.dname end StateName,
	t2.sshowname as LatelyReceName,
	t3.DisPrice KkPrice,t3.ConEnd,t3.SetmealName
from
cu_customers t1
left join tb_sys_users t2 on t1.LatelyReceId = t2.iuserid
left join tb_sys_dictionary t4 on t4.did=t1.CustState and t4.typeid='D01'
left join cu_Contract t3 on t1.ContractId = t3.id
left join (select SUM(Contractprice) Contractprice,SUM(Disprice) Disprice,count(*) sl,{0} Cid from cu_Contract where Cid={0} and state='2' and status in (1,2,3,4,7) ) t5 on t5.Cid=t1.id
left join (select SUM(case when FlowingType IN ('00101001','00201001') then Account else 0 end  ) Djprice,SUM(case when FlowingType IN ('00102001','00202001') then Account else 0 end) Htprice,SUM(case when FlowingType IN ('00103001','00203001') then Account else 0 end) Hdprice,SUM(case when FlowingType IN ('00104001','00204001') then Account else 0 end) Stprice,{0} Cid from cu_Account where Cid={0} and state in (0,1,2,3)  ) t6 on t6.Cid=t1.id
left join (select CourseName,TeacherName,TwoTeacherName, {0} Cid from cu_TeacherCourse where id = (
select top 1 tid from cu_TeacherCourse_Sign where Cid={0} order by id desc
)) t7 on t7.Cid=t1.id
left join 
(select COUNT(*) SignSl,ContractId from  cu_TeacherCourse_Sign where Cid={0} group by ContractId) t9 on t9.ContractId=t1.ContractId
left join
(select COUNT(*) Zsl,  cast(isnull(count(*),0)*1.0/DATEDIFF(week,DATEADD(month,-6,getdate()),GETDATE())   as decimal(10,2)) WeekHksl,{0} Cid from  cu_TeacherCourse_Sign where  Cid={0} and  Godate>=DATEADD(month,-6,getdate()) and Godate<=GETDATE()) t8 on t8.Cid=t1.id
left join 
(select  COUNT(*) Hdsl,{0} Cid from cu_Activity_SignUp where Cid={0}) t10 on t10.Cid=t1.id
where t1.id ={0}
", id);
            DataRow data = BasePage.bd.ExecuteRow(sql);
            ViewBag.data = data;
            ViewBag.id = id;
            ViewBag.keyCode = keyCode;
            return View(BasePage.V("Customer/CustomerMore"));
        }
        //待办事项
        [ActionFilter(IsLogin = true)]
        public JsonResult MessageData(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            String sql = String.Format(@"select id,'' keyCode,title,convert(varchar(10),addTime,120) addTime from cu_cus_message where state=0 and cid={0}", id);
            Dictionary<String, Object>[] data = BasePage.bd.ExecuteDictArray(sql);
            foreach (Dictionary<String, Object> dr in data)
            {
                dr["keyCode"] = Fun.IDCode(dr["id"].ToString());
            }
            return BasePage.Json(ResultData.OK(data));
        }
        [ActionFilter(IsLogin = true)]
        public JsonResult MessageDo(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            String sql = String.Format(@"update cu_cus_message set state=1,doTime=getdate(),doUserId={1} where state=0 and id={0} ", id,BasePage.GetUserId(Session));
            bool t = BasePage.bd.ExecuteSQL(sql);
            return BasePage.Json(t?ResultData.OK("处理成功"):ResultData.Error("处理失败"));
        }
        //跟进记录
        [ActionFilter(IsLogin = true)]
        public JsonResult FllowupData(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            String sql = String.Format(@"select id,t2.dname as AdMode,t3.dname as Cusgrade,Remark 
from 
cu_Cus_followup t1
left join tb_sys_dictionary t2 on t1.AdMode=t2.did and t2.typeid='D03'
left join tb_sys_dictionary t3 on t1.Cusgrade=t3.did and t3.typeid='D05'
where cid={0}", id);
            Dictionary<String, Object>[] data = BasePage.bd.ExecuteDictArray(sql);
            foreach (Dictionary<String, Object> dr in data)
            {
                dr["keyCode"] = Fun.IDCode(dr["id"].ToString());
            }
            return BasePage.Json(ResultData.OK(data));
        }
        //跟进记录
        [ActionFilter(IsLogin = true)]
        public JsonResult LinkRelData(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            String sql = String.Format(@"select a.id,a.LinkName,a.CustTel,case when dname IS null then LinkRelation else dname end dname,IsKids from (
select id,linkname,CustTel,LinkRelation,'否' IsKids from cu_Customers where id={0}
union
select id,linkname1,CustTel1,LinkRelation1,'否' from cu_Customers where len(LinkName1)>0 and  id={0}
union 
select cid,linkname,linktel,linkrelation,case iskids when 1 then '是' else '否' end from cu_Cus_contacts where cu_Cus_contacts.Cid={0}
) a 
left join tb_sys_dictionary on did=LinkRelation and typeid='D04'

", id);
            Dictionary<String, Object>[] data = BasePage.bd.ExecuteDictArray(sql);
            foreach (Dictionary<String, Object> dr in data)
            {
                dr["keyCode"] = Fun.IDCode(dr["id"].ToString());
            }
            return BasePage.Json(ResultData.OK(data));
        }
    }
}